package fr.umlv.m2.jee.pmlv.web.professor;

import fr.umlv.m2.jee.pmlv.persistence.subject.Exam;
import fr.umlv.m2.jee.pmlv.persistence.subject.Lesson;
import fr.umlv.m2.jee.pmlv.persistence.subject.Project;
import fr.umlv.m2.jee.pmlv.persistence.subject.TD;
import fr.umlv.m2.jee.pmlv.persistence.subject.TP;
import fr.umlv.m2.jee.pmlv.service.planning.DisplayRecordDate;
import fr.umlv.m2.jee.pmlv.service.planning.IDisplayRecordDateService;
import fr.umlv.m2.jee.pmlv.service.subject.DisplayAbstractTheme;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplayAbstractThemeService;
import fr.umlv.m2.jee.pmlv.service.user.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet wich represents a confirm's page for a professor.
 * @author Stephanie
 */
public class Confirm extends HttpServlet {
    // Variables
    private User user = null;   //Contains informations about user
    private List<Integer> years = new ArrayList<Integer>(); //Contains years when professor teaching a subject
    private long idTheme = -1;
    private int confirm = -1;
    private DisplayAbstractTheme theme = null;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Confirm() {
            super();
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //Data
        user=null;
        years.clear();
        idTheme = -1;
        confirm = -1;
        theme = null;

        //Retrieve informations about user
        HttpSession session = request.getSession(true);
        user = (User)session.getAttribute("user");
        years.addAll((List<Integer>)session.getAttribute("years"));

        //Retrieve infos about theme
        idTheme = Long.parseLong(request.getParameter("theme"));
        confirm = Integer.parseInt(request.getParameter("confirm"));
        ServletContext context = getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        IDisplayAbstractThemeService themeService = (IDisplayAbstractThemeService) applicationContext.getBean("defautDisplayAbstractThemeService");
        theme = themeService.getAbstractThemeById(idTheme);

        //Redirect to previous page
        if(request.getParameter("ok")!=null)
        {
            if(confirm==0)
                themeService.publishAbstractTheme(idTheme);
            else
            {
                if(theme.getRecordDate()!=null) {
                    IDisplayRecordDateService dateService = (IDisplayRecordDateService) applicationContext.getBean("defautDisplayRecordDateService");
                    DisplayRecordDate record = new DisplayRecordDate(theme.getRecordDate());
                    dateService.deleteRecordDate(record);
                }
                themeService.deleteAbstractTheme(idTheme);
            }
            response.sendRedirect((String)session.getAttribute("back"));
        }
        else if(request.getParameter("cancel")!=null)
        {
            response.sendRedirect((String)session.getAttribute("back"));
        }


        //View
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" >");

        out.println(" <head>");
        out.println("   <title>PMLV : Portail Universitaire de Marne-La-Vall&eacute;e</title>");
        out.println("   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />");
        out.println("   <link rel=\"stylesheet\" media=\"screen\" type=\"text/css\" href=\"../css/professor/professor.css\" />");
        out.println(" </head>");

        out.println(" <body>");

        out.println("   <div id=\"header\">");
        out.println("     <ul>");
        out.println("       <li class=\"menuItem menuItemSpacing\"><a href=\"../Professor\">Accueil</a></li>");
        out.println("       <li class=\"menuItem menuItemSpacing\"><a href=\"ListOfStudents\">Listes des &eacute;tudiants</a></li>");
        out.println("       <li class=\"menuItem menuItemSpacing cursor\">Emploi du temps");
        out.println("         <ul class=\"subMenuBar\">");
        out.println("           <li class=\"subMenu\"><a href=\"ScheduleOfCoursesAndExams\">Cours et examens</a></li>");
        out.println("           <li class=\"subMenu\"><a href=\"ScheduleOfWorksToMake\">Rendu de travaux</a></li>");
        out.println("         </ul>");
        out.println("       </li>");
        out.println("       <li class=\"menuItem menuItemSpacing cursor\">Gestion de mati&egrave;re");
        out.println("         <ul class=\"subMenuBar\">");
        for(Integer year:years)
        {
            out.println("           <li class=\"subMenu\"><a href=\"ManagementOfSubjects?year="+year+"\">"+ year +"-"+ (year+1) +"</a></li>");
        }
        out.println("         </ul>");
        out.println("       </li>");
        out.println("       <li id=\"menuItemMargin\" class=\"menuItem\"><a href=\"../j_spring_security_logout\">Se d&eacute;connecter</a></li>");
        out.println("     </ul>");
        out.println("   </div>");
        
        getDivContent(out, request, response);

        out.println("   <div id=\"footer\">");
        out.println("     <div id=\"copyright\">&copy; Universit&eacute; Paris-Est Marne-la-Vall&eacute;e</div>");
        out.println("     <div>PMLV v1.0 </div>");
        out.println("   </div>");

        out.println(" </body>");

        out.println("</html>");
        out.close();
    }

    /**
     *  Content of page
     */
    private void getDivContent(PrintWriter out, HttpServletRequest request, HttpServletResponse response)
    {
        out.println("   <div id=\"content\">");
        out.println("     <div id=\"title\"> Espace Professeur </div>");
        out.println("           <br/><br/><br/><center>");
        out.println("               <form method=\"get\" action=\"Confirm\">");
        String str_confirm = (confirm==0 ? "Publier" : "Supprimer");
        String str_type_theme = "";
        if(theme.getAbstractTheme() instanceof Lesson) str_type_theme = "le cours";
        else if(theme.getAbstractTheme() instanceof TD) str_type_theme = "le travaux dirig&eacute;s";
        else if(theme.getAbstractTheme() instanceof TP) str_type_theme = "le travaux pratiques";
        else if(theme.getAbstractTheme() instanceof Project) str_type_theme = "le projet";
        else if(theme.getAbstractTheme() instanceof Exam) str_type_theme = "l'examen";
        out.println("                   &Ecirc;tes vous s&ucirc;r de vouloir "+str_confirm.toLowerCase()+" "+str_type_theme+" : \""+theme.getName()+"\" ?<br/><br/>");
        out.println("                   <input type=\"submit\" name=\"ok\" value=\""+str_confirm+"\">");
        out.println("                   <input type=\"submit\" name=\"cancel\" value=\"Annuler\">");
        out.println("                   <input type=\"hidden\" name=\"theme\" value=\""+idTheme+"\">");
        out.println("                   <input type=\"hidden\" name=\"confirm\" value=\""+confirm+"\">");
        out.println("           </form>");
        out.println("       </center>");
        out.println("   </div>");
    }
}